'use strict';
let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let Conopts = {
        autoescape: opts.autoescape || true,
        throwOnUndefined: opts.throwOnUndefined || false,
        lstripBlock: opts.lstripBlock || false,
        trimBlock: opts.trimBlock || false,
        watch: opts.watch || false,
        noCache: opts.noCache || false
    }
    let env = nunjucks.configure(path, Conopts);
    return env;
}

module.exports = async (ctx, next) => {
    ctx.render = (view, model) => {
        let env = createEnv();
        ctx.body = env.render(view,model);
    }
    await next();
}